package property.service.impl;

import org.apache.ibatis.session.SqlSession;
import property.mapper.OverhaulMapper;
import property.pojo.Overhaul;
import property.pojo.OverhaulKey;
import property.service.OverhaulService;
import property.utils.mapper.MyBatisUtil;

import javax.servlet.annotation.WebServlet;

public class OverhaulServiceImpl implements OverhaulService {
    @Override
    public int addOverhaul(Overhaul overhaul) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        OverhaulMapper mapper = sqlSession.getMapper(OverhaulMapper.class);
        int i = 0;
        try{
             i = mapper.insertSelective(overhaul);
        }catch (Exception e){
            if (e.getMessage().toString().contains("PRIMARY") || e.getMessage().toString().contains("FOREIGN KEY")) {
                i = -2;
            }
        }
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int deleteOverhaul(OverhaulKey overhaulKey) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        OverhaulMapper mapper = sqlSession.getMapper(OverhaulMapper.class);
        int i = mapper.deleteByPrimaryKey(overhaulKey);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int updateOverhaul(Overhaul overhaul) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        OverhaulMapper mapper = sqlSession.getMapper(OverhaulMapper.class);
        int i = mapper.updateByPrimaryKey(overhaul);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }
}
